package com.k12.admim.controller.operator;

import cn.dev33.satoken.stp.StpUtil;
import com.k12.admim.model.entity.sys.SysMenu;
import com.k12.admim.model.vo.MenuInfoVo;
import com.k12.admim.model.vo.MenuVo;
import com.k12.admim.rpc.UserFeignClient;
import com.k12.admim.service.OperatorMenuService;
import com.k12.admim.utils.MenuTreeUtil;
import com.k12.admim.utils.RequestContextUtil;
import com.k12.common.model.resp.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class OperatorMenuController {


    @Resource
    private UserFeignClient userFeignClient;


    @Resource
    private OperatorMenuService operatorMenuService;

    /**
     * 获取菜单列表
     * @return R
     */
    @GetMapping("system/menu/list")
    public R listMenu() {
        Object loginId = StpUtil.getLoginId();
        if(null == loginId){
            return R.failed("请先登录");
        }
        String tenantId = RequestContextUtil.getTenantId();
        if(StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        String userId = loginId.toString();
        List<MenuVo> menu = userFeignClient.listMenuInfo(tenantId,userId);
        List<MenuVo> menuVos = MenuTreeUtil.makeListToTree(menu);
        return R.success(menuVos);
    }

    @GetMapping(value = "system/menu/roleMenuTreeselect/{roleId}")
    public R  roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        String tenantId = RequestContextUtil.getTenantId();
        if (com.alibaba.cloud.commons.lang.StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        String loginId = StpUtil.getLoginId().toString();
        List<MenuVo> menus = operatorMenuService.selectMenuList(tenantId,loginId);
        MenuInfoVo menuInfo = new MenuInfoVo();
        menuInfo.setCheckedKeys(operatorMenuService.selectMenuListByRoleId(roleId,tenantId));
        menuInfo.setMenus( MenuTreeUtil.makeListToTree(menus));
        return R.success(menuInfo);
    }


    @PostMapping("system/menu")
    public R add(@Validated @RequestBody SysMenu menu) {
        String tenantId = RequestContextUtil.getTenantId();
        if (com.alibaba.cloud.commons.lang.StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        if (!operatorMenuService.checkMenuNameUnique(menu)) {
            return R.failed("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if (0 == menu.getIsFrame() && !StringUtils.startsWithAny(menu.getPath(), "http://", "https://")) {
            return R.failed("新增菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        }
        String loginId = String.valueOf(StpUtil.getLoginId());
        menu.setCreateBy(loginId);
        return R.success(operatorMenuService.insertMenu(menu));
    }


    @PutMapping("system/menu")
    public R edit(@Validated @RequestBody SysMenu menu) {
        if (!operatorMenuService.checkMenuNameUnique(menu)) {
            return R.failed("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if (0 == menu.getIsFrame() && !StringUtils.startsWithAny(menu.getPath(), "http://", "https://")) {
            return R.failed("修改菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        } else if (menu.getMenuId().equals(menu.getParentId())) {
            return R.failed("修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        String loginId = String.valueOf(StpUtil.getLoginId());
        menu.setUpdateBy(loginId);
        return R.success(operatorMenuService.updateMenu(menu));
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @GetMapping(value = "system/menu/{menuId}")
    public R getInfo(@PathVariable Long menuId) {
        String tenantId = RequestContextUtil.getTenantId();
        if (com.alibaba.cloud.commons.lang.StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        return R.success(operatorMenuService.selectMenuById(menuId,tenantId));
    }


    @DeleteMapping("system/menu/{menuId}")
    public R remove(@PathVariable("menuId") Long menuId) {
        String tenantId = RequestContextUtil.getTenantId();
        if (com.alibaba.cloud.commons.lang.StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        if (operatorMenuService.hasChildByMenuId(menuId,tenantId)) {
            return R.failed("存在子菜单,不允许删除");
        }

        if (operatorMenuService.checkMenuExistRole(menuId,tenantId)) {
            return R.failed("菜单已分配,不允许删除");
        }
        return R.success(operatorMenuService.deleteMenuById(menuId,tenantId));
    }


}
